/**
 * Created by no1 on 2014/8/29.
 */
'use strict';
var mysql = require('../common/mysql.js');
var F = require('../common/function');
var C = require('../config/index');
var commonO2o = require('../common/o2o');
var o2oServiceAPI = require('../common/o2oServiceApi.js');
module.exports = function (app) {
  var self = this;
  var apiPre = C.apiPre;
  //var apiPre=C.o2oApiPre;

  /*
   @todo API01 获取短信验证码
   */
  app.get(apiPre + '/:apiVer/shopList', function*() {
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.test(this);
    }
  });
  /*
   @todo API02 获取地区
   */
  app.post(apiPre + '/:apiVer/district', function*(Env) {
    this.I = yield F.Init(this, [1, 2, 3]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.district(this);
    }
  });
  /*
   @todo API03 获取城市
   */
  app.post(apiPre + '/:apiVer/city', function*(Env) {
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.city(this);
    }
  });

  /*
   @todo API04 获取首页的list
   */
  app.post(apiPre + '/:apiVer/itemList', function*(Env) {
    this.I = yield F.Init(this, [1, 2]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.itemList(this);
    }
  });
  /*
   @todo API05 获取单个商品或店面
   */
  app.post(apiPre + '/:apiVer/getSingleItem', function*(Env) {
    this.I = yield F.Init(this, [1, 2, 3]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.getSingleItem(this);
    }
  });
  //导入商圈
  app.get(apiPre + '/:apiVer/importDistrict', function*() {
    yield commonO2o.importDistrict(this);
  });

  /*
   @todo API06 获取类别
   */
  app.post(apiPre + '/:apiVer/categoryList', function*() {
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.getCategory(this);
    }
  });

  /*
   @todo API07_1 获取商家分店列表
   获取商家其他分店列表
   */
  app.post(apiPre + '/:apiVer/getSubShop', function*() {
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.getSubShop(this);
    }
  });

  /*
   @todo API07_2 获取商品适用分店列表
   获取商家其他分店列表
   */
  app.post(apiPre + '/:apiVer/getWareShop', function*() {
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.getWareShop(this);
    }
  });

  /*
   @todo API08 订单相关接口
   */
  app.post(apiPre + '/:apiVer/orderOperation', function*() {
    this.I = yield F.Init(this, [1, 2, 3, 4, 5]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.orderOperation(this);
    }
  });

  /*
   @todo API09 APP端增加未认证商店接口
   */
  app.post(apiPre + '/:apiVer/addUnautherizedShop', function*() {
    this.I = yield F.Init(this, [1]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.addUnautherizedShop(this);
    }
  });

  /*
   @todo API10 APP端模糊查询商店接口
   */
  app.post(apiPre + '/:apiVer/searchShopByName', function*() {
    this.I = yield F.Init(this, [1,2]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.searchShopByName(this);
    }
  });

  /*
   @todo API11 APP端获取省市区列表
   */
  app.post(apiPre + '/:apiVer/getDistrict', function*() {
    this.I = yield F.Init(this, [1,2]);
    if (this.I.errors) {
      this.jsonp = F.returnMsg(400, this.I.errors.msg, this.I.errors.level);
    } else {
      this.jsonp = yield commonO2o.getDistrict(this);
    }
  });

  app.post(apiPre + '/:apiVer/importShop', function*() {
    yield commonO2o.importShop();
  });


  /**
   * article 相关 TODO
   */
    // TODO 会关闭 get /api/v1/article/test   {...} test
  app.get(apiPre + '/:apiVer/article/test', function*() {
    this.I = yield F.Init(this, []);
    //this.jsonp = 'test works';
    this.jsonp = yield commonO2o.articleTest(this);
  });
  // TODO 连接池测试,会关闭 /api/v1/article/cleanreq
  app.get(apiPre + '/:apiVer/article/cleanreq', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleCleanReq(this);
  });
  // TODO logFlow,会关闭 /api/v1/article/logflow
  app.get(apiPre + '/:apiVer/article/logflow', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleLogFlow(this);
  });
  // TODO logFlow,会关闭 /api/v1/article/logflow2
  app.post(apiPre + '/:apiVer/article/logflow2', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleLogFlow2(this);
  });

  // post /api/v1/article/addBatch  {...} 添加多个帖子
  app.post(apiPre + '/:apiVer/article/addBatch', function*() {
    //this.jsonp = 'addBatch works';
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleAddBatch(this);
  });
  // get /api/v1/article/termfreq?top=100 统计所有帖子的词频
  app.get(apiPre + '/:apiVer/article/termfreq', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleTermfreq(this);
  });
  // get /api/v1/article/rebuild
  app.get(apiPre + '/:apiVer/article/rebuild', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleReBuild(this);
  });
  // get /api/v1/article/index
  app.get(apiPre + '/:apiVer/article/index', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleIndex(this);
  });
  // quick search  get /api/v1/article/qsearch?q=foo&size=10&from=10 快速检索
  app.get(apiPre + '/:apiVer/article/qsearch', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleQSearch(this);
  });
  // TODO [自定义打分机制,会扩展] /api/v1/article/cscore?q=宝宝&add=1&sub=2&mul=3
  app.get(apiPre + '/:apiVer/article/cscore', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleCScore(this);
  });
  // TODO /api/v1/article/autocate?ctype=mannual&top=5&text=宝宝
  app.get(apiPre + '/:apiVer/article/autocate', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleAutocate(this);
  });
  // TODO /api/v1/article/similar?tid=123&top=10&policy=text&debug=1 =>查找tid类似的帖子
  // TODO /api/v1/article/similar?text=blabla&top=10&debug=1 => 查找content内容类似的帖子
  // tid和text必选一个, 找出与tid123内容相似的帖子,blabla是帖子123的内容,
  // 可选policy=[term,默认|text], 开发中,现阶段效果并不好],debug=true会返回articleContent
  // 经过测试发现 text 和 term现阶段的效果都很差 !!
  app.get(apiPre + '/:apiVer/article/similar', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleSimilar(this);
  });
  // /api/v1/article/similar2 查找content内容类似的帖子
  app.post(apiPre + '/:apiVer/article/similar2', function*() {
    this.I = yield F.Init(this, [1,2]);
    this.jsonp = yield commonO2o.articleSimilar2(this);
  });
  // TODO /api/v1/article/termvector?tid=491&debug=1  对491号帖子进行词汇统计
  app.get(apiPre + '/:apiVer/article/termvector', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleTermvector(this);
  });

  // TODO /api/v1/article/analyze?text=blabla&policy=text 词汇分析
  // policy=text/term(default);
  app.get(apiPre + '/:apiVer/article/analyze', function*() {
    this.I = yield F.Init(this, []);
    this.jsonp = yield commonO2o.articleAnalyze(this);
  });

};

